package com.sch.www.common.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 该类来自 org.apache.commons.httpclient.util;
 */

public class DateTimeUtil {
    public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
    public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
    public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
    public static final String PATTERN_DATE = "yyyy-MM-dd";
    public static final String PATTERN_DATE_ZH = "yyyy年MM月dd日";
    public static final String PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_DATETIME_ZH = "yyyy年MM月dd日 HH时mm分ss秒";
    private static final Collection DEFAULT_PATTERNS = Arrays.asList(new String[]{PATTERN_DATETIME,PATTERN_DATE,PATTERN_DATETIME_ZH,PATTERN_DATE_ZH,PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123});
    private static final Date DEFAULT_TWO_DIGIT_YEAR_START;
    private static final TimeZone  timeZone;

    public static Date parseDate(String dateValue) throws Exception {
        return parseDate(dateValue, (Collection)null, (Date)null);
    }

    public static Date parseDate(String dateValue, Collection dateFormats) throws Exception {
        return parseDate(dateValue, dateFormats, (Date)null);
    }

    public static Date parseDate(String dateValue, String dateFormat) throws Exception {

        return parseDate( dateValue, Arrays.asList(new String[]{dateFormat}));
    }

    public static Date parseDate(String dateValue, Collection dateFormats, Date startDate) throws Exception {
        if(dateValue == null) {
            throw new IllegalArgumentException("dateValue is null");
        } else {
            if(dateFormats == null) {
                dateFormats = DEFAULT_PATTERNS;
            }

            if(startDate == null) {
                startDate = DEFAULT_TWO_DIGIT_YEAR_START;
            }

            if(dateValue.length() > 1 && dateValue.startsWith("\'") && dateValue.endsWith("\'")) {
                dateValue = dateValue.substring(1, dateValue.length() - 1);
            }

            SimpleDateFormat dateParser = null;
            Iterator formatIter = dateFormats.iterator();

            while(formatIter.hasNext()) {
                String format = (String)formatIter.next();
                if(dateParser == null) {
                    dateParser = new SimpleDateFormat(format);
                    dateParser.setTimeZone(timeZone);
                    dateParser.set2DigitYearStart(startDate);
                } else {
                    dateParser.applyPattern(format);
                }
                try {

                    return dateParser.parse(dateValue);
                } catch (ParseException var7) {
                    ;
                }
            }

            throw new Exception("Unable to parse the date " + dateValue);
        }
    }



    public static String formatDate(Date date, String pattern) {
        if(date == null) {
            throw new IllegalArgumentException("date is null");
        } else if(pattern == null) {
            throw new IllegalArgumentException("pattern is null");
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            formatter.setTimeZone(timeZone);
            return formatter.format(date);
        }
    }

    private DateTimeUtil() {
    }

    static {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2000, 0, 1, 0, 0,0);
        DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime();
        timeZone=TimeZone.getDefault();
    }

public static void main(String[] args)  throws Exception{


//    System.out.println(parseDate("2017-06-04 12:13:14",PATTERN_DATETIME));
//    System.out.println(parseDate("2017-06-04 12:13:14"));
//    System.out.println(parseDate("2017-06-04"));




      System.out.println(formatDate(parseDate("2017年06月04日 12时13分14秒"),PATTERN_DATETIME_ZH));

      System.out.println(formatDate(parseDate("2017年06月04日"),PATTERN_DATETIME_ZH));





}


}
